package com.sugon.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.AlarmCategoryDao;
import com.sugon.entity.AlarmCategoryEntity;
import com.sugon.service.AlarmCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 报警分类表Service实现类
 *
 * @author sugon
 * @date 2023-03-13 14:49:19
 */
@Service("alarmCategoryService")
public class AlarmCategoryServiceImpl implements AlarmCategoryService {
    @Autowired
    private AlarmCategoryDao alarmCategoryDao;

    @Override
    public AlarmCategoryEntity queryByCategoryCode(String categoryCode) {
        Map<String, Object> param = MapUtil.newHashMap();
        param.put("categoryCode", categoryCode);
        List<AlarmCategoryEntity> alarmCategoryList = queryList(param);
        if (CollectionUtil.isNotEmpty(alarmCategoryList)) {
            return alarmCategoryList.get(0);
        }
        return null;
    }

    @Override
    public List<AlarmCategoryEntity> listByCode(String code) {
        List<AlarmCategoryEntity> alarmCategoryList = alarmCategoryDao.listByCode(code);
        return Optional.ofNullable(alarmCategoryList).orElse(CollectionUtil.newArrayList());
    }

    @Override
    public AlarmCategoryEntity queryObject(String id) {
        return alarmCategoryDao.queryObject(id);
    }

    @Override
    public List<String> listBMS(String alarmCategoryId) {
        List<String> idList = CollectionUtil.newArrayList();
        AlarmCategoryEntity alarmCategory = queryObject(alarmCategoryId);
        if (alarmCategory != null) {
            List<AlarmCategoryEntity> alarmCategoryList = alarmCategoryDao.listTreeByCode(alarmCategory.getCode());
            if (CollectionUtil.isNotEmpty(alarmCategoryList)) {
                idList.addAll(alarmCategoryList.stream().map(AlarmCategoryEntity::getId).collect(Collectors.toList()));
            }
        }
        return idList;
    }

    @Override
    public List<AlarmCategoryEntity> queryList(Map<String, Object> map) {
        return alarmCategoryDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return alarmCategoryDao.queryTotal(map);
    }

    @Override
    public int save(AlarmCategoryEntity alarmCategory) {
        alarmCategory.setId(RandomUtil.randomString(32));
        return alarmCategoryDao.save(alarmCategory);
    }

    @Override
    public int update(AlarmCategoryEntity alarmCategory) {
        return alarmCategoryDao.update(alarmCategory);
    }

    @Override
    public int delete(String id) {
        return alarmCategoryDao.delete(id);
    }

    @Override
    public int deleteBatch(String[] ids) {
        return alarmCategoryDao.deleteBatch(ids);
    }
}
